package com.momo.background.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.momo.background.dto.HotelInfoDto;
import com.momo.background.dto.OrderInfoDto;
import com.momo.background.service.OrderInfoService;
import com.momo.background.vo.AdminVo;
import com.momo.background.vo.HotelInfo;
import com.momo.background.vo.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/background")
public class OrderInfoController {

    @Autowired
    OrderInfoService orderInfoService;

    @RequestMapping(value = "/OrderList",method = RequestMethod.POST)
    public Object OrderList(@RequestBody(required = false) OrderInfoVo orderInfoVo, HttpServletRequest request){
        AdminVo adminVo = (AdminVo) request.getSession().getAttribute("UserInfo");
        String role = adminVo.getRole().toString();
        if (role.equals("1")) {
            PageHelper.startPage(orderInfoVo.getCurrentPage(), orderInfoVo.getPageSize());
            List<OrderInfoDto> orderInfoDtos = orderInfoService.BackgroundOrder(orderInfoVo);
            PageInfo<OrderInfoDto> info = new PageInfo<OrderInfoDto>(orderInfoDtos);
            System.out.println(info.getList());
            return info;
        }
        if (role.equals("0")){
            PageHelper.startPage(orderInfoVo.getCurrentPage(), orderInfoVo.getPageSize());
            orderInfoVo.setHotelId(adminVo.getHotelId());
            List<OrderInfoDto> orderInfoDtos2 = orderInfoService.BackgroundOrder2(orderInfoVo);
            PageInfo<OrderInfoDto> info = new PageInfo<OrderInfoDto>(orderInfoDtos2);
            return info;
        }
        return null;
    }

    @RequestMapping(value = "/delOrder",method = RequestMethod.POST)
    public boolean delOrder(@RequestBody OrderInfoVo orderInfoVo){
        int rSet = orderInfoService.delOrder(orderInfoVo);
        if (rSet!=0){return true;}
        return false;
    }

    @RequestMapping(value = "/delRoom",method = RequestMethod.POST)
    public boolean delRoom(@RequestBody HotelInfo hotelInfo){
        int rSet = orderInfoService.delRoom(hotelInfo);
        if (rSet!=0){return true;}
        return false;
    }
}
